﻿using BroadGoalComponents.BGControls.Toast;
using BroadGoalComponents.Dialogs;
using Custom.Controls;
using Custom.Framework;
using Custom.Model.Entity;
using Custom.Service.Repository;
using Jskp.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Health
{
    public partial class frmHealth : PopupFormWithCover
    {
        private HealthInfoRepository healthDAL = new HealthInfoRepository();
        private int cusid = 0;
        private HealthInfo health = null;

        public frmHealth(int cusid)
        {
            InitializeComponent();

            this.bgDataGridView1.TopLeftHeaderCell.Value = "序号";
            this.bgDataGridView1.RowHeadersWidth = 55;
            this.bgDataGridView1.RowHeadersVisible = true;
            this.bgDataGridView1.ShowLineNo = true;

            this.bgDataGridView1.TopHeaders = new BroadGoalComponents.BGControls.MergeHeaderSetting[]
            {
                new BroadGoalComponents.BGControls.MergeHeaderSetting
                {
                    Index = this.col_refractionR.Index,
                    Span = this.col_correctR.Index - this.col_refractionR.Index + 1,
                    Text = "右眼",
                    TopAreaHeight = 36,
                    TopTextFont = new Font("微软雅黑", 12, FontStyle.Bold, GraphicsUnit.Pixel)
                },
                new BroadGoalComponents.BGControls.MergeHeaderSetting
                {
                    Index = this.col_refractionL.Index,
                    Span = this.col_correctL.Index - this.col_refractionL.Index + 1,
                    Text = "左眼",
                    TopAreaHeight = 36,
                    TopTextFont = new Font("微软雅黑", 12, FontStyle.Bold, GraphicsUnit.Pixel)
                }
            };

            this.cusid = cusid;
            this.btnChange.Visible = false;
            this.btnSave.Location = new Point((this.Width - this.btnSave.Width) / 2, this.btnSave.Location.Y);

            this.txtrefractionR.BackColor = Color.FromArgb(255, 255, 192);
            this.txtastigmiaR.BackColor = Color.FromArgb(255, 255, 192);
            this.txtaxialR.BackColor = Color.FromArgb(255, 255, 192);
            this.txtcorrectR.BackColor = Color.FromArgb(255, 255, 192);
            this.txtrefractionL.BackColor = Color.FromArgb(192, 255, 192);
            this.txtastigmiaL.BackColor = Color.FromArgb(192, 255, 192);
            this.txtaxialL.BackColor = Color.FromArgb(192, 255, 192);
            this.txtcorrectL.BackColor = Color.FromArgb(192, 255, 192);
            this.txtdistance.BackColor = Color.FromArgb(192, 192, 255);
        }

        private void frmHealth_Load(object sender, EventArgs e)
        {
            this.search();
            this.lbstatus.Text = "（正在新增...）";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.checkData()) return;

            if (this.health == null)
            {
                this.addSave();
            }
            else
            {
                this.editSave();
            }
        }

        /// <summary>
        /// 操作列按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgDataGridView1_ActionButtonClick(object sender, BroadGoalComponents.BGControls.ActionButtonEventArgs e)
        {
            int rowIndex = this.bgDataGridView1.CurrentRow.Index;

            switch (e.ButtonName)
            {
                case "修改":
                    this.edit(rowIndex);
                    break;
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            this.init();
            this.lbcreatetimehead.Visible = false;
            this.lbcreatetime.Visible = false;
        }

        /// <summary>
        /// 数据检查
        /// </summary>
        /// <returns></returns>
        private bool checkData()
        {
            //if (string.IsNullOrWhiteSpace(this.txtrefractionR.Text))
            //{
            //    WarningDialog.Show("请填写右眼屈光值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtastigmiaR.Text))
            //{
            //    WarningDialog.Show("请填写右眼散光值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtaxialR.Text))
            //{
            //    WarningDialog.Show("请填写右眼轴位值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtcorrectR.Text))
            //{
            //    WarningDialog.Show("请填写右眼矫正值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtrefractionL.Text))
            //{
            //    WarningDialog.Show("请填写左眼屈光值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtastigmiaL.Text))
            //{
            //    WarningDialog.Show("请填写左眼散光值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtaxialL.Text))
            //{
            //    WarningDialog.Show("请填写左眼轴位值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtcorrectL.Text))
            //{
            //    WarningDialog.Show("请填写左眼矫正值");
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(this.txtdistance.Text))
            //{
            //    WarningDialog.Show("请填写瞳距值");
            //    return false;
            //}

            return true;
        }

        private void addSave()
        {
            this.health = new HealthInfo
            {
                cusid = this.cusid,
                refractionR = this.txtrefractionR.Text.Trim(),
                astigmiaR = this.txtastigmiaR.Text.Trim(),
                axialR = this.txtaxialR.Text.Trim(),
                correctR = this.txtcorrectR.Text.Trim(),
                refractionL = this.txtrefractionL.Text.Trim(),
                astigmiaL = this.txtastigmiaL.Text.Trim(),
                axialL = this.txtaxialL.Text.Trim(),
                correctL = this.txtcorrectL.Text.Trim(),
                distance = this.txtdistance.Text.Trim(),
                createman = "",
                createtime = DateTime.Now
            };

            try
            {
                this.healthDAL.Insert(health);
                this.init();
                this.search();
                BGToast.Show("保存成功");
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                WarningDialog.Show("保存出错。" + ex.Message);
            }
        }

        private void editSave()
        {
            this.health.refractionR = this.txtrefractionR.Text.Trim();
            this.health.astigmiaR = this.txtastigmiaR.Text.Trim();
            this.health.axialR = this.txtaxialR.Text.Trim();
            this.health.correctR = this.txtcorrectR.Text.Trim();
            this.health.refractionL = this.txtrefractionL.Text.Trim();
            this.health.astigmiaL = this.txtastigmiaL.Text.Trim();
            this.health.axialL = this.txtaxialL.Text.Trim();
            this.health.correctL = this.txtcorrectL.Text.Trim();
            this.health.distance = this.txtdistance.Text.Trim();

            try
            {
                this.healthDAL.Update(health);
                this.init();
                this.search();
                BGToast.Show("保存成功");
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                WarningDialog.Show("保存出错。" + ex.Message);
            }
        }

        private void search()
        {
            List<HealthInfo> healths = this.healthDAL.Get(w => w.cusid == this.cusid).ToList();

            this.bgDataGridView1.DataSource = healths;
        }

        private void init()
        {
            this.health = null;
            this.txtrefractionR.Text = "";
            this.txtastigmiaR.Text = "";
            this.txtaxialR.Text = "";
            this.txtcorrectR.Text = "";
            this.txtrefractionL.Text = "";
            this.txtastigmiaL.Text = "";
            this.txtaxialL.Text = "";
            this.txtcorrectL.Text = "";
            this.txtdistance.Text = "";
            this.lbcreatetime.Text = "----";
            this.lbstatus.Text = "（正在新增...）";
            this.btnChange.Visible = false;
            this.btnSave.Location = new Point(454, 489);

            for (int i = 0; i < this.bgDataGridView1.RowCount; i++)
            {
                this.bgDataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.FromArgb(64, 64, 64);
                this.bgDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.FromArgb(64, 64, 64);
            }
        }

        private void edit(int rowIndex)
        {
            int id = int.Parse(this.bgDataGridView1.Rows[rowIndex].Cells[this.col_id.Name].Value.ToString());
            this.health = this.healthDAL.Get(id);

            this.txtrefractionR.Text = health.refractionR?.ToString();
            this.txtastigmiaR.Text = health.astigmiaR?.ToString();
            this.txtaxialR.Text = health.axialR?.ToString();
            this.txtcorrectR.Text = health.correctR?.ToString();
            this.txtrefractionL.Text = health.refractionL?.ToString();
            this.txtastigmiaL.Text = health.astigmiaL?.ToString();
            this.txtaxialL.Text = health.axialL?.ToString();
            this.txtcorrectL.Text = health.correctL?.ToString();
            this.txtdistance.Text = health.distance?.ToString();
            this.lbcreatetime.Text = ((DateTime)health.createtime).ToString("yyyy-MM-dd HH:mm:ss");
            this.lbcreatetimehead.Visible = true;
            this.lbcreatetime.Visible = true;
            this.lbstatus.Text = "（正在编辑...）";
            this.btnChange.Visible = true;
            this.btnSave.Location = new Point(502, this.btnSave.Location.Y);

            for (int i = 0; i < this.bgDataGridView1.RowCount; i++)
            {
                if (i == rowIndex)
                {
                    this.bgDataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Green;
                    this.bgDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Green;
                }
                else
                {
                    this.bgDataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.FromArgb(64, 64, 64);
                    this.bgDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.FromArgb(64, 64, 64);
                }
            }
        }

        private void bgDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex >= this.col_refractionR.Index && e.ColumnIndex <= this.col_correctR.Index)
            {
                e.CellStyle.BackColor = Color.FromArgb(255, 255, 192);
                e.CellStyle.SelectionBackColor = Color.FromArgb(255, 255, 192);
                //if (Double.Parse(e.Value?.ToString() ?? "0") < 0)
                //{
                //    e.CellStyle.ForeColor = Color.Red;
                //    e.CellStyle.SelectionForeColor = Color.Red;
                //}
            }
            else if (e.ColumnIndex >= this.col_refractionL.Index && e.ColumnIndex <= this.col_correctL.Index)
            {
                e.CellStyle.BackColor = Color.FromArgb(192, 255, 192);
                e.CellStyle.SelectionBackColor = Color.FromArgb(192, 255, 192);
                //if (Double.Parse(e.Value?.ToString() ?? "0") < 0)
                //{
                //    e.CellStyle.ForeColor = Color.Red;
                //    e.CellStyle.SelectionForeColor = Color.Red;
                //}
            }
            else if (e.ColumnIndex == this.col_distance.Index)
            {
                e.CellStyle.BackColor = Color.FromArgb(192, 192, 255);
                e.CellStyle.SelectionBackColor = Color.FromArgb(192, 192, 255);
                //if (Double.Parse(e.Value?.ToString() ?? "0") < 0)
                //{
                //    e.CellStyle.ForeColor = Color.Red;
                //    e.CellStyle.SelectionForeColor = Color.Red;
                //}
            }
        }
    }
}
